home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_dev.lha / GMSDev / Source / C / StartDPK / StartDPK.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-18  |  5.2 KB  |  204 lines

  1. /* Name:       StartDPK
  2. ** Template 1: StartDPK <TaskFile> [-p <Name>] <Arg1> <Arg2> ...
  3. ** Template 2: StartDPK <TaskFile> [PREFS=<Name>] <Arg1> <Arg2> ...
  4. ** Author:     Paul Manias
  5. ** Copyright:  DreamWorld Productions (c) 1997-1998.  All rights reserved.
  6. ** Date:       January 1998
  7. ** Docs:       This program is intended to allow multi-platform capabilities by
  8. **             doing things like opening the kernel.  For example the following
  9. **             code would not work on a Mac even if it is 68000 code:
  10. **
  11. **              move.l $4.w,a6
  12. **              ...
  13. **              CALL   OpenLibrary
  14. **
  15. **            To fix this problem we put this machine-specific code in a task
  16. **            launcher (StartDPK) and pass the DPKBase onto the task.  Alakazam,
  17. **            multiple platform capabilities!
  18. **
  19. ** PROBLEMS:  Programs written in E cannot cope with this as far as I know :-(.
  20. */
  21.  
  22. #include <proto/exec.h>
  23. #include <proto/dos.h>
  24. #include <proto/dpkernel.h>
  25. #include <workbench/startup.h>
  26. #include <system/tasks.h>
  27. #include <clib/icon_protos.h>
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33.  
  34. extern struct ExecBase *SysBase;
  35. extern struct Library  *IconBase;
  36.  
  37. /*** Proto-types ***/
  38.  
  39. void PrintUsage(void);
  40. void Launch(void);
  41. __asm LaunchTask(register __a0 struct Segment *);
  42.  
  43. /*** Program variables ***/
  44.  
  45. BYTE *TaskFile = NULL;
  46. BYTE *Prefs    = NULL;
  47. struct Segment *Segment = NULL;
  48. struct StartUp *StartUp = NULL;
  49. struct FileName file    = { ID_FILENAME, NULL };
  50.  
  51. /************************************************************************************
  52. ** Main code.
  53. */
  54.  
  55. LONG main(LONG argc, BYTE *argv[])
  56. {
  57.   struct WBStartup  *WBMsg;
  58.   struct WBArg      *WBArg;
  59.   struct DiskObject *dobj;
  60.   BYTE   *str;
  61.   LONG   PrefsSize = NULL;
  62.  
  63.   if (argc IS NULL) {
  64.      /**** Launched from WorkBench ****/
  65.  
  66.      WBMsg = (struct WBStartup *)argv;
  67.      WBArg = WBMsg->sm_ArgList;
  68.  
  69.      /* Skip StartDPK and program location */
  70.  
  71.      WBArg++;
  72.  
  73.      /* Process filename and location */
  74.  
  75.      printf("Loading: %s\n",WBArg->wa_Name);
  76.      file.Name = WBArg->wa_Name;
  77.      CurrentDir(WBArg->wa_Lock);
  78.  
  79.      /* Process tool types */
  80.  
  81.      if (IconBase = OpenLibrary("icon.library", 0)) {
  82.         if (dobj = GetDiskObject(WBArg->wa_Name)) {
  83.            if (str = FindToolType(dobj->do_ToolTypes, "PREFS")) {
  84.               if (Prefs = AllocMem(strlen(str)+1,NULL)) {
  85.                  PrefsSize = strlen(str)+1;
  86.                  strcpy(Prefs, str);
  87.               }
  88.            }
  89.            FreeDiskObject(dobj);
  90.         }
  91.         else printf("Error in getting DiskObject.\n");
  92.  
  93.         CloseLibrary((struct Library *)IconBase);
  94.      }
  95.      else printf("Could not open icon.library.\n");
  96.   }
  97.   else {
  98.      /**** Launched from CLI ****/
  99.  
  100.      if ((argc < 2) OR (argv[1][0] IS '?')) {
  101.         PrintUsage();
  102.         return(NULL);
  103.      }
  104.  
  105.      printf("Loading: %s\n",argv[1]);
  106.      file.Name = argv[1];             /* Task is always specified in argument 1 */
  107.  
  108.      /* Check for preferences */
  109.  
  110.      if (argc > 2) {
  111.         if (argc > 3) {
  112.            if (stricmp("-p",argv[2]) IS NULL) {
  113.               Prefs = argv[3];
  114.            }
  115.         }
  116.         else {
  117.            if (strnicmp("PREFS=",argv[2],6) IS NULL) {
  118.               Prefs = argv[2] + 6;
  119.            }
  120.         }
  121.      }
  122.   }
  123.  
  124.   /* Open the library, launch the program and then
  125.   ** shut down before exiting.
  126.   */
  127.  
  128.   if (DPKBase = (struct DPKBase *)OpenLibrary("GMS:libs/dpkernel.library",0)) {
  129.      Launch();
  130.      CloseDPK();
  131.   }
  132.   else printf("Could not open the dpkernel.library.  Have you installed it?\n");
  133.  
  134.   if (PrefsSize) FreeMem(Prefs,PrefsSize);
  135.  
  136.   return(NULL);
  137. }
  138.  
  139. /***********************************************************************************/
  140.  
  141. void PrintUsage(void)
  142. {
  143.   printf("\nSTARTDPK\n");
  144.   printf("--------\n");
  145.   printf("This program will launch DPK tasks for you, and in future will\n");
  146.   printf("be required for setting up programs that have been compiled on other\n");
  147.   printf("platforms.\n\n");
  148.   printf("To use it, just type:\n\n");
  149.   printf("  1> StartDPK <file> [-p prefsname] [Arg1] [Arg2] [Arg3] ...\n\n");
  150.   printf("Example:\n");
  151.   printf("  1> StartDPK DPK:demos/Redimension -p Settings1\n\n");
  152. }
  153.  
  154. /************************************************************************************
  155. **
  156. **
  157. **
  158. */
  159.  
  160. #define PREFSLEN 10
  161.  
  162. void Launch(void)
  163. {
  164.   struct DPKTask *Task;
  165.   WORD i,j;
  166.  
  167.   if (Task = FindDPKTask()) {
  168.      if (Prefs) {
  169.         if (Task->Preferences = AllocMemBlock(PREFSLEN+strlen(Prefs)+2,MEM_DATA)) {
  170.            strcpy(Task->Preferences,"GMS:Prefs/");
  171.            j = NULL;
  172.            i = PREFSLEN;
  173.            while (Prefs[j] != NULL) {
  174.               Task->Preferences[i++] = Prefs[j++];
  175.            }
  176.            Task->Preferences[i++] = '/';
  177.            Task->Preferences[i]   = NULL;
  178.  
  179.            printf("Preferences: %s\n",Task->Preferences);
  180.         }
  181.         else {
  182.            printf("Low memory error.");
  183.            return;
  184.         }
  185.      }
  186.      else {
  187.         printf("Using default preferences.\n");
  188.      }
  189.  
  190.      if (Segment = Load(&file,ID_SEGMENT)) {
  191.         LaunchTask(Segment);
  192.         Free(Segment);
  193.      }
  194.      else printf("Sorry, the file that you specified does not exist.\n");
  195.  
  196.      if (Prefs) {
  197.         FreeMemBlock(Task->Preferences);
  198.         Task->Preferences = NULL;
  199.      }
  200.   }
  201.   else printf("Could not find DPK task, error.\n");
  202. }
  203.  
  204.